package hu.edudroid.sipguide;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.ParseException;
import java.util.Enumeration;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.os.Bundle;
import android.util.Log;

public class ServerActivity extends Activity {
	
	public SipManager mSipManager = null;
	public SipAudioCall call = null;
	public ServerReceiver callReceiver = null;
	private SipProfile mSipProfile = null;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		IntentFilter filter = new IntentFilter();
        filter.addAction("android.SipDemo.INCOMING_CALL");
		callReceiver = new ServerReceiver();
        this.registerReceiver(callReceiver, filter);
        
    	if (mSipManager == null) {
    		mSipManager = SipManager.newInstance(this);
    	}
    	
    	try {
            if (mSipProfile != null) {
            	mSipManager.close(mSipProfile.getUriString());
            }
        } catch (Exception ee) {
            Log.d("WalkieTalkieActivity/onDestroy", "Failed to close local profile.", ee);
        }
    	
    	try {
		
			SipProfile.Builder builder = new SipProfile.Builder("sip:server@"+getLocalIpAddress());
			Log.e("IP",getLocalIpAddress());
			mSipProfile = builder.build();
			
			Intent intent = new Intent();
            intent.setAction("android.SipDemo.INCOMING_CALL");
            PendingIntent pendingintent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);
            mSipManager.open(mSipProfile, pendingintent, null);
            
            SipRegistrationListener sl=new SipRegistrationListener() {
                public void onRegistering(String localProfileUri) {
                	Log.e("Registering with SIP Server...", localProfileUri);
                }

                public void onRegistrationDone(String localProfileUri, long expiryTime) {
                	Log.e("Ready", localProfileUri);
                }

                public void onRegistrationFailed(String localProfileUri, int errorCode,
                        String errorMessage) {
                	Log.e("Registration failed.  Please check settings.", errorMessage);
                }
            };
            
            mSipManager.register(mSipProfile, 100, sl);
			
		} catch (SipException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
	}
	
	protected void onDestroy(){
		super.onDestroy();
		if (callReceiver != null) {
            this.unregisterReceiver(callReceiver);
        }
	}
	
 	public String getLocalIpAddress() {
	    try {
	        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
	            NetworkInterface intf = en.nextElement();
	            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
	                InetAddress inetAddress = enumIpAddr.nextElement();
	                if (!inetAddress.isLoopbackAddress()) {
	                    return inetAddress.getHostAddress().toString();
	                }
	            }
	        }
	    } catch (SocketException ex) {
	        Log.e("", ex.toString());
	    }
	    return null;
	}
	
}
